package com.stylefeng.guns.rest.common;

import com.stylefeng.guns.api.user.vo.UserInfoModel;

/**
 * @Author: jiaru
 * @Description:    当前登录用户
 * @Date: Created on 2019/3/17
 * @Modify:
 */
public class CurrentUser {
    // 线程绑定储存空间
    private static final ThreadLocal<UserInfoModel> THREAD_LOCAL = new ThreadLocal<>();
    // 线程绑定储存空间
    private static final ThreadLocal<String> THREAD_LOCAL_SLIGHT = new ThreadLocal<>();

    /**
     * 保存用户id到线程中
     * @param userId     用户id
     */
    public static void setCurrentUserId(String userId) {
        THREAD_LOCAL_SLIGHT.set(userId);
    }

    /**
     * 获取线程存储空间的用户id
     * @return
     */
    public static String getCurrentUserId() {
        return THREAD_LOCAL_SLIGHT.get();
    }

    /**
     * 删除线程存储空间的用户id
     * @return
     */
    public static void removeCurrentUserId() {
        THREAD_LOCAL_SLIGHT.remove();
    }


    /**
     * 保存用户信息到线程中
     * @param model     用户信息
     */
    public static void setCurrentUser(UserInfoModel model) {
        THREAD_LOCAL.set(model);
    }

    /**
     * 获取线程存储空间的用户信息
     * @return
     */
    public static UserInfoModel getCurrentUser() {
        return THREAD_LOCAL.get();
    }

    /**
     * 删除线程存储空间的用户信息
     * @return
     */
    public static void removeCurrentUser() {
        THREAD_LOCAL.remove();
    }
}
